#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    priority_queue<ll, vector<ll>, greater<ll> > pq;
    ll n; cin >> n;
    for (int i = 1; i <= n; i++) {
        ll x; cin >> x;
        pq.push(x);
    }
    ll ans = 0;
    while (pq.size() > 1) {
        ll x = pq.top(); pq.pop();
        ll y = pq.top(); pq.pop();
        ans += x + y;
        pq.push(x + y);
    }
    cout << ans << '\n';
    return 0;
}